package com.ysstech.common.service;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

@Configuration
public class SendMailService {
    // Spring官方提供的集成邮件服务的实现类，目前是Java后端发送邮件和集成邮件服务的主流工具。
    @Autowired
    private JavaMailSender javaMailSender;

    // 从配置文件中注入发件人的姓名
    @Value("${spring.mail.properties.mail.display.sendmail}")
    private String sendmail;
    @Value("${spring.mail.properties.mail.display.sendname}")
    private String sendName;

    /**
     * 发送邮件
     *
     * @param to      接受人
     * @param subject 主题
     * @param content 发送内容
     * @param ishtml  发送内容
     * @throws MessagingException           异常
     * @throws UnsupportedEncodingException 异常
     */
    public void sendMail(String to, String subject, String content, boolean ishtml) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(sendmail, sendName);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, ishtml);
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       接受人
     * @param subject  主题
     * @param content  发送内容
     * @param filePath 附件路径
     * @throws MessagingException           异常
     * @throws UnsupportedEncodingException 异常
     */
    public void sendAttachmentMail(String to, String subject, String content, boolean ishtml, String filePath) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(sendmail, sendName);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, ishtml);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        messageHelper.addAttachment(fileName, file);
        javaMailSender.send(mimeMessage);
    }


    /**
     * 发送带附件的邮件
     *
     * @param to      接受人
     * @param subject 主题
     * @param content 发送内容
     * @param file    附件
     * @throws MessagingException           异常
     * @throws UnsupportedEncodingException 异常
     */
    public void sendAttachmentMail(String to, String subject, String content, boolean ishtml, File file) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(sendmail, sendName);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, ishtml);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        String fileName = file.getName();
        fileName = fileName.substring(fileName.lastIndexOf(File.separator));
        messageHelper.addAttachment(fileName, fileSystemResource);
        javaMailSender.send(mimeMessage);
    }
}
